
import { createSlice } from "@reduxjs/toolkit";
import { localRequest,request } from "@/utils";
import { 
    getCourseListAPI,
    getVideoListAPI,
    getExamTestAPI, 
    getCheckListAPI,
    getInfoAPI,
    getExamListAPI, 
    getSituationAPI 
} from "@/apis/student";

const studentStore = createSlice({
    name:'student',
    initialState:{
        courseList:[],
        testCheck:[],
        testItem:[],
        testList:[],
        videoList:[],
        learnCondition:{},
        info:{}
    },
    reducers:{
        setCourseList(state,action){
            state.courseList = action.payload;
        },
        setTestCheck(state,action){
            state.testCheck = action.payload;
        },
        setTestItem(state,action){
            state.testItem = action.payload;
        },
        setTestList(state,action){
            state.testList = action.payload;
        },
        setVideoList(state,action){
            state.videoList = action.payload;
        },
        setLearnConditon(state,action){
            state.learnCondition = action.payload;
        },
        setInfo(state,action){
            state.info = action.payload;
        }
    }
})

const {
    setCourseList, 
    setTestCheck, 
    setTestItem, 
    setTestList,
    setVideoList, 
    setLearnConditon,
    setInfo
} = studentStore.actions;

const studentReducer = studentStore.reducer;

// 获取课程列表请求
const fetchGetCourseList = function(token){
    return async function(dispatch){
        const res = await getCourseListAPI(token);
        console.log("get courselist",res.data);
        // 添加标识符
        for(let i = 0; i < res.data.length; i++){
            res.data[i].id = i;
        }
        dispatch(setCourseList(res.data));
        return res.data;
    }
}


const fetchGetTestCheck = function(data,token){
    return async function(dispatch){
        const res = await getCheckListAPI(data,token);
        console.log("get checklist",res.data);
        dispatch(setTestCheck(res.data.map((ele,index) => ({
            ...ele,
            key:index
        }))));
        
    }
}


const fetchGetTestItem = function(data,token){
    return async function(dispatch){
        const res = await getExamTestAPI(data,token);
        console.log("get test list",res.data);
        dispatch(setTestItem(res.data.map((ele,index) => ({
            ...ele,
            key:index
        }))));
        
    }
}

const fetchGetTestList = function(class_id,token){
    // console.log(class_id,token);
    return async function(dispatch){
        const res = await getExamListAPI(class_id,token)
        console.log("get testlist",res.data);
        dispatch(setTestList(res.data));
        
    }
}

const fetchGetVideoList = function(class_id,token){
    return async function(dispatch){
        const res = await getVideoListAPI(class_id,token);
        console.log("get videolist",res.data);
        dispatch(setVideoList(res.data));
    }
}

const fetchGetLearnCondition = function(class_id,token){
    return async function(dispatch){
        const res = await getSituationAPI(class_id,token);
        console.log("get conditon",res.data);
        dispatch(setLearnConditon(res.data));
    }
}

const fetchGetInfo = function(token){
    return async function(dispatch){
        const res = await getInfoAPI(token);
        console.log("get info",res.data);
        dispatch(setInfo(res.data));
    }
}


export {
    setCourseList, 
    setTestCheck, 
    setTestItem, 
    setTestList,
    setVideoList, 
    setLearnConditon,
    fetchGetCourseList,
    fetchGetTestCheck,
    fetchGetTestItem,
    fetchGetTestList,
    fetchGetVideoList,
    fetchGetLearnCondition,
    fetchGetInfo
}

export default studentReducer;